<?php
/**
 * 前台 企业联系方式 控制器
 *
 */
class EcontactusAction extends Action
{
	/**
	 * 网站统一配置
	 *
	 * @var unknown_type
	 */
	public $Config;
	
	/**
	 * 构造函数
	 *
	 */
	public function _initialize()
	{
		$Config = D('Config');
		$this->Config = $Config->find('config_id=1');
	}
	/**
	 * 查看联系方式
	 */
	public function Show()
	{
		$Input = Input::getInstance();
		$Input->filter('addslashes');
		//设置查询条件
		import('ORG.Util.HashMap');
		$conditions = new HashMap();
		$conditions->put('enter_id',array('EQ',$Input->get('enter_id')));
		
		$Contactus = D('Econtactus');
		$C = $Contactus->where($conditions)->find();
		
		$conditions = NULL;
		//获取企业信息
		//设置条件
		$conditions = new HashMap();
		$conditions->put('enter_id',array('EQ',$Input->get('enter_id')));
		
		$Enterprise = D('Enterprise');
		$Enter = $Enterprise->where($conditions)->find();
		//组成模板 xxx@Index:Show
    	$UserTpl = $Enter['templates'] . '@Econtactus:Show';
		
		//To tpl
		$this->assign('C',$C);
		$this->assign('Enterprise',$Enter);
		$this->assign('enter_id',$Input->get('enter_id'));
		$this->assign('Config',$this->Config);
		$this->display($UserTpl);
	}
    /**
     * 判断是否有联系方式
     * 并进行创建或修改跳转
     */
    public function UserIndex()
    {
    	EasyACL('Econtactus','UserIndex',$_SESSION);
    	//计算数据库中是否有该企业的联系方式
    	import('ORG.Util.HashMap');
    	$conditions = new HashMap();
    	$conditions->put('enter_id',array('EQ',$_SESSION['enter_id']));
    	
    	$Contactus = D('Econtactus');
    	$count = $Contactus->count($conditions,'e_id');
    	if ($count > 0) {
    		$this->redirect('UserUpdate','Econtactus');
    	} else {
    		$this->redirect('UserCreate','Econtactus');
    	}
    }
    
    /**
     * 创建页面
     */
    public function UserCreate()
    {
    	EasyACL('Econtactus','UserCreate',$_SESSION);
    	//POST
    	if ($this->isPost()) {
    		$Contact = D('Econtactus');
    		if ($Contact->create()) {
    			if ($Contact->add()) {
    				return $this->success('成功修改联系方式');
    			} else {
    				return $this->error($Contact->getError());
    			}
    		} else {
    			return $this->error($Contact->getError());
    		}
    	}
    	//To templates
    	$this->assign('Config',$this->Config);
    	$this->display();
    }
    
    /**
     * 修改联系方式
     */
    public function UserUpdate()
    {
    	EasyACL('Econtactus','UserUpdate',$_SESSION);
    	$Contactus = D('Econtactus');
    	
    	//POST
    	if ($this->isPost()) {
    		$Input = Input::getInstance();
    		$Input->filter('addslashes');
    		$condition = array();
    		$condition['e_id'] = $Input->post('e_id');
    		$Contactus->where($condition)->find();
    		if ($Contactus->create()) {
    			if ($Contactus->save()) {
    				return $this->success('修改成功!');
    			} else {
    				return $this->error($Contactus->getError());
    			}
    		} else {
    			return $this->error($Contactus->getError());
    		}
    	}
    	
    	//获取企业的联系方式
    	$condition = array();
    	$condition['enter_id'] = $_SESSION['enter_id'];
    	
    	$C = $Contactus->where($condition)->find();
    	
    	//To templates
    	$this->assign('C',$C);
    	$this->assign('Config',$this->Config);
    	$this->display();
    }
}
?>







